package org.example.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.common.Result;
import org.example.mapper.BusinessMapper;
import org.example.mapper.GoodsMapper;
import org.example.pojo.Business;
import org.example.pojo.Chart;
import org.example.pojo.Goods;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("chart")
public class ChartController {
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @PostMapping("/chart1")
    public Result getchart(){
        LambdaQueryWrapper<Business> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        List<Business> businessList=businessMapper.selectList(lambdaQueryWrapper);

        List<Chart> chartList=new ArrayList<>();

        for (int i = 0; i < businessList.size(); i++) {
            LambdaQueryWrapper<Goods> lambdaQueryWrapper1=new LambdaQueryWrapper<>();

            lambdaQueryWrapper1.eq(Goods::getBusinessId,businessList.get(i).getId());
            System.out.println(goodsMapper.selectList(lambdaQueryWrapper1));
            chartList.add(new Chart(businessList.get(i).getName(),goodsMapper.selectCount(lambdaQueryWrapper1)));
        }
        for (int i = 0; i < chartList.size(); i++) {
            System.out.println(chartList.get(i).getName());
            System.out.println(chartList.get(i).getCount());

        }



        return Result.ok(chartList);
    }
}
